/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package compevol;

import java.io.File;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.data.xy.XYDataset;

/**
 *
 * @author user
 */
public class GeradorGrafico{

    private JFreeChart chart;
    public GeradorGrafico(PieDataset dataset, String titulo) {
        // based on the dataset we create the chart
        chart = createChart(dataset, titulo);
        // Salva uma copia do grafico localmente
        saveChart(chart);
    }

    public JFreeChart getChart() {
        return chart;
    }

    private PieDataset createDataset() {
        DefaultPieDataset result = new DefaultPieDataset();
        result.setValue("Linux", 29);
        result.setValue("Mac", 20);
        result.setValue("Windows", 51);
        return result;
        
    }
    private JFreeChart createChart(PieDataset dataset, String title) {
        
        JFreeChart chart = ChartFactory.createPieChart3D(
            title,  				// chart title
            dataset,                // data
            true,                   // include legend
            true,
            false
        );

        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setForegroundAlpha(0.5f);
        return chart;
        
    }
    /**
     * Salva uma copia do grafico localmente, funciona esta sendo
     * usado pra verificar a saida.
     */
    private void saveChart(JFreeChart chart)
    {
        String fileName="C:/Users/user/Downloads/pieChart.jpg";
        try {
            /**
             * This utility saves the JFreeChart as a JPEG
             * First Parameter: FileName
             * Second Parameter: Chart To Save
             * Third Parameter: Height Of Picture
             * Fourth Parameter: Width Of Picture
             */
            ChartUtilities.saveChartAsJPEG(new File(fileName), chart, 800, 600);
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("Problem occurred creating chart.");
        }
    }
}
